<template>
  <div class="shopcar">
    <div class="content" @click="togglefold">
      <div class="contentleft">
        <div class="logowrap">
          <div class="logocircle">
            <div class="logo" :class="{'highlight':totalCount>0}">
              <img src="./shopcar.png" alt="">
            </div>
            <div class="foodsnum" v-show="totalCount">{{totalCount}}</div>
          </div>
        </div>
        <div class="money" :class="{'highlight':totalCount>0}">￥{{totalPrice}}</div>
        <div class="deliverinfo">另需配送费￥{{deliveryPrice}}元</div>
      </div>
      <div class="contentright" :class="{'gotopay':payStatus === '去结算'}">{{payStatus}}</div>
    </div>
    <div class="ballwrap" v-for="ball in balls">
      <!--<transition name="drop" v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:after-enter="afterEnter">-->
      <div class="ball"></div>
      <!--</transition>-->
      <div class="inner inner-hook"></div>
    </div>
    <div class="shop-list-wrap" v-show="shoplist">
      <div class="list-header">
        <h1 class="tit">购物车</h1>
        <span class="empty" @click="emptyClick">清空</span>
      </div>
      <div class="list-content" ref="listContent">
        <ul>
          <li class="food" v-for="food in selectFoods">
            <span class="name">{{food.name}}</span>
            <div class="price">
              <span>￥{{food.price*food.count}}</span>
            </div>
            <div class="cartcontrol-wrap">
              <cartcontrol :food="food"></cartcontrol>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="mark" v-show="markshow" @click="markClick"></div>
  </div>
</template>
<script type="text/ecmascript-6">
  import Hub from '../../components/commit/hub';
  import cartcontrol from '../../components/cartcontrol/cartcontrol.vue';
  import BScroll from 'better-scroll';

  export default{
    props: {
      //计算购物车价钱,需要在goods页面中告诉我们的,保存选择商品的数组
      selectFoods: {
        type: Array,
        //默认的状态
        default() {
          return [
            {
              price: 10,
              count: 4
            }
          ];
        }
      },
      deliveryPrice: {
        type: Number,
        defaule: 0
      },
      minPrice: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        balls: [
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          }
        ],
        dropBall: [],
        fold:true,
        mark:true
      }
    },
    //计算
    computed: {
      totalPrice() {
        let total = 0;
        this.selectFoods.forEach((food) => {
          total += food.price * food.count;
        })
        return total;
      },
      totalCount() {
        let count = 0;
        this.selectFoods.forEach((food) => {
          count += food.count;
        })
        return count;
      },
      payStatus(){
        if (this.totalPrice === 0) {
          //es6 反引号  不需要字符串拼接；${}的方式
          return `￥${this.minPrice}元起送`;
        } else if (this.totalPrice < this.minPrice) {
          let diff = this.minPrice - this.totalPrice;
          return `还差￥${diff}元起送`;
        } else {
          return '去结算';
        }
      },
      shoplist(){
        if(!this.totalCount){
          this.fold=true;
          return false;
        }
        let show= !this.fold;
        if(show){
          this.$nextTick(() => {
            if(!this.scroll){
              this.scroll = new BScroll(this.$refs.listContent,{
                click:true
              });
            } else {
              this.scroll.refresh();
            }
          })
        }
        return show;
      },
      markshow(){
        if(this.fold){
          this.mark=true;
          return false
        }
        let show=!this.mark;
        return show;
      }
    },
    created () {
      Hub.$on('stringsAdd', (target) => {
        console.log(target);
      })
    },
    methods: {
      drop(el) {
        for (let i = 0; i < this.balls.length; i++) {
          let ball = this.balls[i];
          if (!ball.show) {
            ball.show = true;
            ball.el = el;
            this.dropBall.push(ball);
            return;
          }
        }
      },
      togglefold(){
        if(!this.totalCount) {
          return;
        }
        this.fold = !this.fold;
        this.mark=! this.mark;
      },
      markClick(){
        this.mark=! this.mark;
        this.fold = !this.fold;
      },
      emptyClick(){
        this.selectFoods.forEach((food) => {
          food.count = 0;
        })
      }
    },
//    transitions:{
//      drop: {
//        beforeEnter:function(el) {
//          let count=this.balls.length;
//          while (count--){
//            let ball=this.balls[count];
//            if(ball.show){
//              let rect=ball.getBoundingClientRect();
//              let x=rect.left-32;
//              let inner=el.getElementsByClassName('inner-hook')[0];
//              let y=-(window.innerHeight-rect.top-22);
//              el.style.display='';
//              el.style.webkitTransform=`translate3d(0,${y}px,0)`;
//              el.style.transform=`translate3d(0,${y}px,0)`;
//              inner.style.webkitTransform=`translate3d(${x}px,0,0)`
//              inner.style.transform=`translate3d(${x}px,0,0)`;
////              Velocity(el,{transform:'translate3d(0,-100px,0)'},{duration:500})
////              Velocity(inner,{transform:'translate3d(-100px,0,0)'},{duration:500})
//            }
//          }
//        },
//        enter:function(el,done){
//          /* eslint-idsable no-unused-vars */
//          let rf=el.offestHeight;
//          this.$nextTick(() =>{
//            let inner=el.getElementsByClassName('inner-hook')[0];
//            el.style.webkitTransform='translate3d(0,0,0)';
//            el.style.transform='translate3d(0,0,0)';
//            inner.style.inner='translate3d(0,0,0)';
//            inner.style.transform='translate3d(0,0,0)';
////            Velocity(el,{transform:'translate3d(0,0,0)'},{duration:500})
////            Velocity(inner,{transform:'translate3d(0,0,0)'},{duration:500})
////            Velocity(el,{transform:'translate3d(0,-100px,0)'},{complete:done})
//          });
//        },
//        afterEnter:function(el){
//          let ball=this.dropBalls.shift();
//          if(ball){
//            ball.show=false;
//            el.style.display='none';
//          }
//        }
//      }
//    }
    components: {
      cartcontrol
    }
  }
</script>

<style>
  .shopcar{width: 100%;position: fixed;bottom: 0px;height: 45px;background-color: #141d27;z-index :40}
  .content{display: flex;z-index: 100;}
  .content .contentleft{flex: auto;position: relative;}
  .contentleft .logowrap{width: 50px;height: 45px;margin-left: 4px;float: left;}
  .contentleft .logocircle{display: inline-block;position: absolute;height: 58px;border-radius: 50%;width: 58px;background-color: #141d27;bottom: 0px;left: 4px;
    z-index: 100;}
  .contentleft .logocircle .logo{width: 44px;height: 44px;border-radius: 50%;position: absolute;bottom: 7px;left: 7px;background-color: rgba(255, 255, 255, 0.4);}
  .contentleft .logocircle .logo.highlight{background-color: rgb(0, 160, 220);}
  .contentleft .logocircle .logo img{display: inline-block;margin: 10px 7px;width: 30px;height: 25px;}
  .contentleft .logocircle .foodsnum{display: inline-block;width: 25px;height: 16px;background-color: red;position: absolute;right: 0;border-radius: 8px;font-size: 10px;text-align: center;color: #fff;line-height: 16px;}
  .contentleft .money{width: auto;height: 30px;display: inline-block;font-size: 14px;font-weight: 700;text-align: center;line-height: 30px;color: #919396;float: left;padding: 0 10px;margin-top: 7px;border-right: 1px solid #7e8c8d;}
  .contentleft .money.highlight{color: #fff;}
  .contentleft .deliverinfo{display: inline-block;width: auto;padding: 0 5px;height: 45px;line-height: 45px;color: #7e8c8d;font-size: 12px;}
  .contentright{width: 105px;height: 45px;background-color: #2b333b;color: #979a9c;font-weight: 700;font-size: 10px;line-height: 45px;text-align: center;}
  .contentright.gotopay{background-color: #00b43c;color: #fff;font-weight: 700;}
  .ballwrap .ball{display: inline-block;width: 16px;height: 16px;position: fixed;left: 32px;bottom: 22px;z-index: 200;}
  .ballwrap .inner{width: 16px;height: 16px;border-radius: 50%;background-color: rgb(0, 160, 220);}
  /*折叠列表*/
  .shop-list-wrap{width: 100%;display: block;max-height:258px;position: fixed;bottom:45px;/*overflow-y: scroll;*/overflow: hidden;background-color: #fff;z-index: 50;}
  .shop-list-wrap .list-header{height:40px;background-color: #f3f5f7;padding:0 5%;border-bottom:1px solid #dbdee1;}
  .shop-list-wrap .list-header .tit{display: inline-block;float: left;height:40px;line-height:40px;font-size: 16px;color: #2a3037;}
  .shop-list-wrap .list-header .empty{height:40px;line-height:40px;float: right;color:rgb(0, 160, 220);font-size: 14px;}
  .list-content .food{display: block;width: 90%;margin:0 5%;height:45px;line-height:45px;border-bottom:1px solid #dbdee1;}
  .list-content .food:last-child{border:none;}
  .list-content .food .name{display: block;float: left;width:55%;height:45px;font-size: 14px;text-overflow: ellipsis;white-space: nowrap;overflow: hidden;}
  .list-content{max-height:200px;overflow: hidden;}
  .list-content .price{display: inline-block;float: left;padding: 0 5%;color: red;}
  .list-content .price span{font-weight:bold;font-size: 14px;}
  .cartcontrol-wrap{width:75px;height:25px;float: right;margin-top: 11px;}
  /*遮罩层*/.mark{position:fixed;top:0;width: 100%;height: 100%;background-color:rgba(7,17,27,0.5);backdrop-filter:blur(7px);
           z-index:-1;}
  @media only screen and (max-width: 321px){.list-content .price{padding: 0 3%;}
    .list-content .food .name{width:50%;}}
</style>
